<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getListData">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection">
			</el-table-column>
			<el-table-column type="index" fixed>
			</el-table-column>
			<el-table-column prop="companyName" label="公司名称"sortable>
			</el-table-column>
			<el-table-column prop="companyTel" label="公司座机" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" :formatter="formatState" sortable>
			</el-table-column>
			<el-table-column prop="address" label="公司地址"  sortable>
			</el-table-column>
			<el-table-column prop="coordinate" label="公司坐标"  sortable>
			</el-table-column>
			<el-table-column prop="logoUrl" label="公司logo"  sortable>
				<template scope="scope">
					<img :src="scope.row.logoUrl" width="40" height="40" class="head_pic"/>
				</template>
			</el-table-column>
			<el-table-column prop="isPay" label="是否付款"  sortable>
			</el-table-column>
			<el-table-column prop="setMeal" label="已选套餐" :formatter="formatSetMeal" sortable>
			</el-table-column>
			<el-table-column label="操作" fixed="right" width="160">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-form-item label="公司名" prop="companyName">
					<el-input v-model="editForm.companyName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="公司座机" prop="companyTel">
					<el-input v-model="editForm.companyTel" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="入驻时间" prop="registerTime">
					<el-input v-model="editForm.registerTime" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="editForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item prop="logo" label="公司Logo">
					<el-input type="hidden" v-model="editForm.logoUrl" auto-complete="off" placeholder="请输入地址！"></el-input>
					<el-upload
							class="upload-demo"
							action="http://localhost:8060/tenant/upload"
							:on-success="handleUploadSuccess"
							:on-remove="handleUploadRemove"
							list-type="picture">
						<el-button size="small" type="success">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>
				<el-form-item prop="address" label="公司地址">
					<el-button @click="openAddress" round>选择地址</el-button>
					<el-input type="text" v-model="editForm.address" auto-complete="off" placeholder="请输入地址！"></el-input>
				</el-form-item>
				<el-dialog title="选择地址" :visible.sync="mapDialogVisible"  :close-on-click-modal="false">
					<template>
						<baidu-map class="bm-view">
							<bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 2100}">
								<div style="margin-bottom:10px">
									<input  id="searchInput" type="text" placeholder="请输入关键字" class="searchinput"width="150"/>
									<el-button @click="confirmAdd" type="success" >确定</el-button>
								</div>
							</bm-auto-complete>
							<bm-view class="bmap"/>
							<bm-local-search :keyword="keyword" :auto-viewport="true" :panel="false"></bm-local-search>
						</baidu-map>
						<baidu-map class="map" style="height: 500px" center="重庆奉节">
							<bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
						</baidu-map>
					</template>
				</el-dialog>
				<el-form-item label="状态">
					<el-select v-model="editForm.state" placeholder="请选择状态">
						<el-option label="付费" value="1"></el-option>
						<el-option label="注册" value="0"></el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>


	</section>
</template>

<script>
    import util from '../../../common/js/util'
    //import NProgress from 'nprogress'
    //import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

    export default {
        data() {
            return {
				mapDialogVisible:false,
                filters: {
                    name: ''
                },
                listData: [],
                total: 0,
                keyword:'',
                pageSize: 10,
                currentPage:1,
                listLoading: false,
                sels: [],//列表选中列
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
/*                editFormRules: {
					username: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' }
					],
					tel: [
						{ required: true, message: '请输入电话', trigger: 'blur' }
					],
					email: [
						{ required: true, message: '请输入邮箱', trigger: 'blur' }
					],
                },*/
                //编辑界面数据
                editForm: {
                	id:0,
					companyName:'',
					companyTel:'',
					email:'',
					registerTime:'',
					state:'',
					address:'',
					logoUrl:'',
					isPay:0
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
					tel: [
						{ required: true, message: '请输入电话', trigger: 'blur' }
					],
					email: [
						{ required: true, message: '请输入邮箱', trigger: 'blur' }
					],
                },
                //新增界面数据
                addForm: {
					id: 0,
					username: '',
					password:'',
					tel:'',
					email:'',
					state:0,
                }

            }
        },
        methods: {
			confirmAdd(){
				this.employee.address =  document.getElementById('searchInput').value;
				this.mapDialogVisible = false;
			},
			openAddress(){
				console.debug(this.keyword)
				this.mapDialogVisible = true;
			},
			handleUploadSuccess(res, file){
				console.log(res.data)
				this.editForm.logoUrl = res.data;
			},
			handleUploadRemove(res, file){
			},
            //性别显示转换
            formatState: function (row, column) {
                return row.state == 1 ? '付费' : row.state == 0 ? '注册' : '欠费';
            },
			formatSetMeal: function (row, column) {
				let setMels = row.setMeal;
				console.log(setMels);
				let setMealNames = new Array();
				for (let i = 0;i<setMels.length;i++){
					setMealNames.push(setMels[i].setMealName)
				}
				setMealNames.join("|");
				return setMealNames.toString();
			},
            handleCurrentChange(val) {
                this.currentPage=val;
                this.getListData();
            },
            //获取用户列表
            getListData() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                    keyword: this.filters.name
                };
                this.listLoading = true;
                this.$http.post('/tenant/findByPage',para).then(res=>{
                    let ajaxResult =  res.data
                    if (ajaxResult.success){
                        this.listData = ajaxResult.data.result;
                        this.total = ajaxResult.data.total
                    }else {
                        this.$message({
                            message: '加载失败',
                            type: 'error'
                        });
                    }
                    this.listLoading = false;
                }).catch(error=>{
                    this.listLoading = false;
                    this.$message({
                        message: '加载失败',
                        type: 'error'
                    });
                })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete('/tenant/delete/'+row.id,{}).then(res=>{
                        let ajaxResult =  res.data
                        if (ajaxResult.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getListData();
                        }else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.listLoading = false;
                    }).catch(error=>{
                        this.listLoading = false;
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    })

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
				this.$http.get('/tenant/findById/'+row.id,{}).then(res=>{
					let ajaxResult = res.data
					console.log(res);
					if (ajaxResult.success){
						this.editForm.id = ajaxResult.data.id;
						this.editForm.companyName = ajaxResult.data.companyName;
						this.editForm.companyTel = ajaxResult.data.companyTel;
						this.editForm.email = ajaxResult.data.email;
						this.editForm.registerTime = ajaxResult.data.registerTime;
						this.editForm.state = ajaxResult.data.state;
						this.editForm.address = ajaxResult.data.address;
						this.editForm.logoUrl = ajaxResult.data.logoUrl;
						this.editForm.isPay = ajaxResult.data.isPay;
						this.editLoading = false;
					}else {
						this.message({
							message:"获取失败",
							type:'error'
						})
					}
				}).catch(error=>{
					this.$message({
						message:"获取失败",
						type:'success'
					})
				})
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    sn:''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            this.$http.post("/tenant/update",para).then(res =>{
                                let ajaxResult = res.data
                                if (ajaxResult.success){
                                    this.$message({
                                        message:"修改成功",
                                        type:'success'
                                    })
                                    this.$refs['editForm'].resetFields();
									this.editLoading = false;
                                    this.editFormVisible = false;
                                    this.getListData();
                                }else {
                                    this.message({
                                        message:"修改失败",
                                        type:'error'
                                    })

                                }
                            }).catch(error=>{
                                this.$message({
                                    message:"修改失败",
                                    type:'success'
                                })
                            })
                            /*para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            editUser(para).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getListData();
                            });*/
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //准备参数，将参数拷贝到para中
                            let para = Object.assign({}, this.addForm);
                            this.$http.post('/employee/add',para).then(res =>{
                                let ajaxResult = res.data
                                if (ajaxResult.success){
                                    this.$message.success("保存成功");
                                    this.addFormVisible = false;
                                    this.$refs['addForm'].resetFields();
                                    this.getListData();
                                }else {
                                    this.addFormVisible = false;
                                    this.$message({
                                        message: '保存失败',
                                        type: 'error'
                                    });
                                }
                                this.addLoading = false;
                            }).catch(error =>{
                                this.addLoading = false;
                                this.$message({
                                    message: '保存失败',
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id)
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { ids: ids };
                    this.$http.post("/employee/batchDelete",para).then(res=>{
                        let ajaxResult = res.data;
                        if (ajaxResult.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getListData();
                        }else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                        this.listLoading = false;
                    }).catch(error => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    })

                })
            }
        },
        mounted() {
            this.getListData();
        }
    }

</script>

<style scoped>

</style>